
package com.tech.arno.spicy.db;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import com.tech.arno.spicy.staticVal.SpicyApplication;


/**
 * @author fangyan
 * @description 配置文件工具类
 * @date 2015年1月15日
 */
public class SharedPreferenceManager {

    private volatile static SharedPreferences sp = null;
    private volatile static SharedPreferenceManager spManager = null;
    private volatile static Editor editor = null;
    /**
     * Preference文件名
     */
    private static final String SHARE_PREFREENCE_NAME = "imooc.pre";

    /**
     * 上次基金更新时间
     */
    public static final String LAST_UPDATE_PRODUCT = "last_update_product";
    public static final String VIDEO_PLAY_SETTING = "video_play_setting";

    private SharedPreferenceManager() {
        //获取sp对象
        sp = SpicyApplication.getInstance().getSharedPreferences(SHARE_PREFREENCE_NAME, Context.MODE_PRIVATE);
        editor = sp.edit();
    }

    //让获取实例的方法进行同步 dcl模式
    public static SharedPreferenceManager getInstance() {
        if (spManager == null || sp == null || editor == null) {
            synchronized (SharedPreferenceManager.class) {
                if (spManager == null || sp == null || editor == null) {
                    spManager = new SharedPreferenceManager();
                }
            }
        }
        return spManager;
    }

    /**
     * @param key
     * @param value
     * @name putInt
     * @function 存放一个int值
     * @author ArnoFrost
     * @time 2017/12/16 0016  16:00
     */
    public void putInt(String key, int value) {
        editor.putInt(key, value);
//        editor.commit();
        editor.apply();
    }

    /**
     * @param key
     * @param defaultValue
     * @return int
     * @name getInt
     * @function 根据key获取int值
     * @author ArnoFrost
     * @time 2017/12/16 0016  16:01
     */
    public int getInt(String key, int defaultValue) {
        return sp.getInt(key, defaultValue);
    }

    /**
     * @param key
     * @param value
     * @name putLong
     * @function 存放一个long值
     * @author ArnoFrost
     * @time 2017/12/16 0016  16:00
     */
    public void putLong(String key, Long value) {
        editor.putLong(key, value);
//        editor.commit();
        editor.apply();
    }

    /**
     * @param key
     * @param defaultValue
     * @return long
     * @name getLong
     * @function 根据key获取long值
     * @author ArnoFrost
     * @time 2017/12/16 0016  16:01
     */
    public long getLong(String key, int defaultValue) {
        return sp.getLong(key, defaultValue);
    }

    /**
     * @param key
     * @param value
     * @name putString
     * @function 存放一个String值
     * @author ArnoFrost
     * @time 2017/12/16 0016  16:00
     */
    public void putString(String key, String value) {
        editor.putString(key, value);
//        editor.commit();
        editor.apply();
    }

    /**
     * @param key
     * @param defaultValue
     * @return string
     * @name getString
     * @function 根据key获取string值
     * @author ArnoFrost
     * @time 2017/12/16 0016  16:01
     */
    public String getString(String key, String defaultValue) {
        return sp.getString(key, defaultValue);
    }

    /**
     * @param key
     * @param value
     * @name putFloat
     * @function 存放一个float值
     * @author ArnoFrost
     * @time 2017/12/16 0016  16:00
     */
    public void putFloat(String key, float value) {
        editor.putFloat(key, value);
//        editor.commit();
        editor.apply();
    }

    /**
     * @param key
     * @param defaultValue
     * @return float
     * @name getFloat
     * @function 根据key获取float值
     * @author ArnoFrost
     * @time 2017/12/16 0016  16:01
     */
    public float getFloat(String key, float defaultValue) {
        return sp.getFloat(key, defaultValue);
    }

    /**
     * @param key
     * @param value
     * @name putBoolean
     * @function 存放一个boolean值
     * @author ArnoFrost
     * @time 2017/12/16 0016  16:00
     */
    public void putBoolean(String key, boolean value) {
        editor.putBoolean(key, value);
//        editor.commit();
        editor.apply();
    }

    /**
     * @param key
     * @param defaultValue
     * @return boolean
     * @name getBoolean
     * @function 根据key获取boolean值
     * @author ArnoFrost
     * @time 2017/12/16 0016  16:01
     */
    public boolean getBoolean(String key, boolean defaultValue) {
        return sp.getBoolean(key, defaultValue);
    }

    public boolean isKeyExist(String key) {
        return sp.contains(key);
    }

    /**
     * @param key
     * @name remove
     * @function 根据Key移除值
     * @author ArnoFrost
     * @time 2017/12/16 0016  16:03
     */
    public void remove(String key) {
        editor.remove(key);
//        editor.commit();
        editor.apply();
    }
}
